package com.NHL.modules.map
{
	import com.NHL.modules.core.BasicContainer;
	import com.NHL.modules.map.vo.MapVo;
	import com.NHL.single.ESource;
	import com.NHL.single.MapLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	public class MapBackGround extends BasicContainer
	{
		private var WIDTH:int = 200;
		private var HEIGHT:int = 200;
		private var _bmdArr:Vector.<BitmapData>;
		private var _bmp:Bitmap;
		private var _bmd:BitmapData;
		private var _mapVo:MapVo;
		public function MapBackGround()
		{
			super();
		}
		
		public function get mapVo():MapVo
		{
			return _mapVo;
		}
		
		public function set mapVo(value:MapVo):void
		{
			if(value!= _mapVo)				
			{
				_mapVo = value;
				//mapInit();
				_bmdArr = ESource.getInstance().getMap(_mapVo.id);
				if(_bmdArr.length > 0)
				{
					mapInit();
				}
				else
				{
					MapLoader.getInstance().add(_mapVo,loadedMap);
				}
			}
			
		}
		
		private function loadedMap():void
		{
			_bmdArr = ESource.getInstance().getMap(_mapVo.id);
			if(_bmdArr.length == 0)
			{
				throw new Error("can't find it")
			}
			mapInit();
		}
		
		private function mapInit():void
		{
			_bmd.dispose();
			if(_bmdArr.length > 0)
			{
				_bmd = new BitmapData(_mapVo.width,_mapVo.height);
				var mtx:Matrix = new Matrix();
				var w:int = Math.ceil(_mapVo.width/WIDTH);
				var h:int = Math.ceil(_mapVo.height/HEIGHT);
				for(var i:int = 0;i < w;i++)
					for(var j:int = 0;j < h;j++)
					{
						mtx.identity();
						mtx.tx = i*WIDTH;
						mtx.ty  = j*HEIGHT;
						_bmd.draw(_bmdArr[i][j],mtx);
					}				
			}
			_bmp.bitmapData = _bmd;
		}
		
		override protected function init():void
		{
			_bmdArr = new Array();
			_bmd = new BitmapData();
		}
	}
}